package com.lagou.game

import scala.io.StdIn
import scala.util.Random

class User() {
    var name: String = _
    var score: Int = _

    def showFist(win: Int, draw: Int, lose: Int) = {

        println(s"$name    $score       $win       $draw        $lose")
    }
}

class Computer() {
    var score: Int = _
    var name: String = _

    def showFist(win: Int, draw: Int, lose: Int) = {

        println(s"$name    $score       $win       $draw        $lose")
    }
}

class Game(partyA: User, partyB: Computer) {
    var cont: Int = _

    def init() = {
        println("*********************************")
        println("***********猜拳，开始************")
        println("*********************************")
        println()

        partyA.name = "游客"
        partyA.score = 0

        println("请选择对战角色：(1.刘备    2.关羽    3.张飞)")
        var num: Int = StdIn.readInt()
        while (num != 1 && num != 2 && num != 3) {
            println("没有这个选项，请重新输入：")
            num = StdIn.readInt()
        }
        partyB.name = num match {
            case 1 => "刘备"
            case 2 => "关羽"
            case 3 => "张飞"
        }
        partyB.score = 0
        println(s"你选择了与${partyB.name} 进行对战")
    }

    def begin(confirm: String) = {
        if (confirm == "y") true else false
    }

    def doGame() = {
        println("要开始了吗？y/n")
        var confirm: String = StdIn.readLine()
        var userWinCont: Int = 0
        var userDrawCont : Int = 0
        while (begin(confirm)) {
            println("请出拳！ 1.剪刀 2.石头 3.布")
            val numUser: Int = StdIn.readInt()
            var gustureUser: String = numUser match {
                case 1 => "剪刀"
                case 2 => "石头"
                case 3 => "布"
                case _ => "其它"
            }
            if (gustureUser == "其它") {
                println("输入不符合规范，默认出布！")
                gustureUser = "布"
            }

            println(s"你出拳:$gustureUser")

            val numComputer: Int = new Random().nextInt(2) + 1

            var gustureComputer: String = numComputer match {
                case 1 => "剪刀"
                case 2 => "石头"
                case 3 => "布"
            }

            println(s"${partyB.name} 出拳：$gustureComputer")

            if (numUser - numComputer == 1 || numUser - numComputer == -2) {
                partyA.score += 2
                userWinCont += 1
                println("结果：恭喜你赢了")
            } else {
                if (numUser == numComputer) {
                    partyA.score += 1
                    partyB.score += 1
                    userDrawCont += 1
                    println("结果：和局！ 下次继续努力！")
                } else {
                    partyB.score += 2
                    println(s"结果：很遗憾！ ${partyB.name} 赢了！")
                }
            }
            cont += 1
            println("是否开始下一轮（y/n）")
            confirm = StdIn.readLine()
        }
        println("退出游戏！")
        println("-----------------------------------------")
        println(s"${partyB.name} VS ${partyA.name}")
        println(s"对战次数$cont 次")
        println()
        println(raw"姓名    得分    胜局    和局    负局")
        partyA.showFist(userWinCont,userDrawCont,cont-userWinCont-userDrawCont)
        partyB.showFist(cont-userWinCont-userDrawCont,userDrawCont,userWinCont)
    }
}

object GuessingGame {
    def main(args: Array[String]): Unit = {

        val user: User = new User()
        val computer: Computer = new Computer()
        val game: Game = new Game(user, computer)

        game.init()
        game.doGame()
    }

}
